| CONTENTS |
The Java programming language is supposed to become the premier tool for connecting computers over the Internet and corporate intranets and, in this realm, Java mostly lives up to the hype. If you are accustomed to programming network connections in C or C++, you will be pleasantly surprised at how easy it is to program them in the Java programming language. For example, as you saw in the applet chapter in Volume 1, it is easy to open a URL (uniform resource locator) on the Net: simply pass the URL to the showDocument method in the AppletContext class.
We begin this chapter by talking a little bit about basic networking. Then, we move on to reviewing and extending the information that was briefly presented in the applet chapter in Volume 1. The rest of the chapter moves on to the intricacies of doing sophisticated work on the Net with the Java programming language. For example, we give you a glimpse into server-side programming with Java. In particular, we show you how to use a combination of an applet and a servlet to harvest information on the Internet.
In the first part of this chapter, we assume that you have no network programming experience. If you have written TCP/IP programs before and ports and sockets are no mystery to you, you should breeze through the sample code. Toward the end of this chapter, the code becomes complex and is geared more toward those with some experience in network programming.
Before writing our first network program, let's learn about a great debugging tool for network programming that you already have, namely, telnet. Most systems (both UNIX and Windows) always come with telnet. However, it is optional with some installations, and you may not have requested it when you installed the operating system. If you can't run telnet from a command shell, then you need to add it from your operating system installation disk.
You may have used telnet to connect to a remote computer and to check your e-mail, but you can use it to communicate with other services provided by Internet hosts as well. Here is an example of what you can do. Type
telnet time-A.timefreq.bldrdoc.gov 13
As Figure 3-1 shows, you should get back a line like this:

52088 01-06-28 14:35:13 50 0 0 644.8 UTC(NIST) *
What is going on? You have connected to the "time of day" service that most UNIX machines constantly run. The particular server that you connected to is operated by the National Institute of Standards and Technology in Boulder, Colorado, and gives the measurement of a Cesium atomic clock. (Of course, the reported time is not completely accurate due to network delays.) By convention, the "time of day" service is always attached to "port" number 13.
NOTE
![]() |
In network parlance, a port is not a physical device, but an abstraction to facilitate communication between a server and a client (see Figure 3-2). Figure 3-2. A client connecting to a server port
|
What is happening is that the server software is continuously running on the remote machine, waiting for any network traffic that wants to chat with port 13. When the operating system on the remote computer gets a network package that contains a request to connect to port number 13, it wakes up the listening server process and establishes the connection. The connection stays up until it is terminated by one of the parties.
When you began the telnet session with time-A.timefreq.bldrdoc.gov at port 13, an unrelated piece of network software knew enough to convert the string "time-A.timefreq.bldrdoc.gov" to its correct Internet Protocol address, 132.163.4.102. The software then sent a connection request to that computer, asking for a connection to port 13. Once the connection was established, the remote program sent back a line of data and then closed the connection. In general, of course, clients and servers engage in a more extensive dialog before one or the other closes the connection.
Here is another experiment, along the same lines, that is a bit more interesting. Do the following:
On Windows, turn on the local key echo in the Terminal Preferences dialog box.
Connect to java.sun.com on port 80.
Type the following, exactly as it appears, without pressing backspace.
GET / HTTP/1.0
Now, press the ENTER key two times.
Figure 3-3 shows the response. It should look eerily familiar—you got a page of HTML-formatted text, namely, the main web page for Java technology.

This is exactly the same process that your web browser goes through to get a web page.
Our first network program in Example 3-1 will do the same thing we did using telnet—connect to a port and print out what it finds.
1. import java.io.*;
2. import java.net.*;
3.
4. /**
5. This program makes a socket connection to the atomic clock
6. in Boulder, Colorado, and prints the time that the
7. server sends.
8. */
9. public class SocketTest
10. {
11. public static void main(String[] args)
12. {
13. try
14. {
15. Socket s = new Socket("time-A.timefreq.bldrdoc.gov",
16. 13);
17.
18. BufferedReader in = new BufferedReader
19. (new InputStreamReader(s.getInputStream()));
20. boolean more = true;
21. while (more)
22. {
23. String line = in.readLine();
24. if (line == null)
25. more = false;
26. else
27. System.out.println(line);
28. }
29.
30. }
31. catch (IOException e)
32. {
33. e.printStackTrace();
34. }
35. }
36. }
This program is extremely simple, but before we analyze the two key lines, note that we are importing the java.net package and catching any input/output errors because the code is encased in a try/catch block. (Since many things can go wrong with a network connection, most of the network-related methods threaten to throw I/O errors. You must catch them for the code to compile.)
As for the code itself, the key lines are as follows:
Socket s = new Socket("time-A.timefreq.bldrdoc.gov", 13);
BufferedReader in = new BufferedReader
(new InputStreamReader(s.getInputStream()));
The first line opens a socket, which is an abstraction for the network software that enables communication out of and into this program. We pass the remote address and the port number to the socket constructor. If the connection fails, then an UnknownHostException is thrown. If there is another problem, then an IOException occurs. Since UnknownHostException is a subclass of IOException and this is a sample program, we just catch the superclass.
Once the socket is open, the getInputStream method injava.net.Socket returns an InputStream object that you can use just like any other stream. (See Chapter 12 of Volume 1.) Once you have grabbed the stream, this program simply
Reads all characters sent by the server using readLine;
Prints each line out to standard output.
This process continues until the stream is finished and the server disconnects. You know this happens when the readLine method returns a null string.
NOTE
![]() |
This program works only with very simple servers, such as a "time of day" service. In more complex networking programs, the client sends request data to the server, and the server may not immediately disconnect at the end of a response. You will see how to implement that behavior in several examples throughout this chapter. |
Plainly, the Socket class is pleasant and easy to use because Java technology hides the complexities of establishing a networking connection and sending data across. The java.net package essentially gives you the same programming interface you would use to work with a file.
Now that we have implemented a basic network client that receives data from the Net, let's implement a simple server that can send information out to the Net. Once you start the server program, it waits for some client to attach to its port. We chose port number 8189, which is not used by any of the standard services. The ServerSocket class is used to establish a socket. In our case, the command
ServerSocket s = new ServerSocket(8189);
establishes a server that monitors port 8189. The command
Socket incoming = s.accept();
tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made. You can use this object to get an input reader and an output writer from that socket, as is shown in the following code:
BufferedReader in = new BufferedReader (new InputStreamReader(incoming.getInputStream())); PrintWriter out = new PrintWriter (incoming.getOutputStream(), true /* autoFlush */);
Everything that the server sends to the server output stream becomes the input of the client program, and all the output from the client program ends up in the server input stream.
In all of the examples in this chapter, we will transmit text through sockets. We, therefore, turn the streams into readers and writers. Then, we can use the readLine method (defined in BufferedReader, but not in InputStream) and the print method (defined in PrintWriter, but not in OutputStream). If we wanted to transmit binary data, we would turn the streams into DataInputStream and DataOutputStreams. To transmit serialized objects, we would use ObjectInputStream and ObjectOutputStreams instead.
Let's send the client a greeting:
out.println("Hello! Enter BYE to exit.");
When you use telnet to connect to this server program at port 8189, you will see the above greeting on the terminal screen.
In this simple server, we just read the client input, a line at a time, and echo it. This demonstrates that the program gets the client's input. An actual server would obviously compute and return an answer that depended on the input.
String line = in.readLine();
if (line != null)
{
out.println("Echo: " + line);
if (line.trim().equals("BYE")) done = true;
}
else done = true;
In the end, we close the incoming socket.
incoming.close();
That is all there is to it. Every server program, such as an HTTP web server, continues performing this loop:
It gets a command from the client ("get me this information") through an incoming data stream.
It somehow fetches the information.
It sends the information to the client through the outgoing data stream.
Example 3-2 is the complete program.
1. import java.io.*;
2. import java.net.*;
3.
4. /**
5. This program implements a simple server that listens to
6. port 8189 and echoes back all client input.
7. */
8. public class EchoServer
9. {
10. public static void main(String[] args )
11. {
12. try
13. {
14. // establish server socket
15. ServerSocket s = new ServerSocket(8189);
16.
17. // wait for client connection
18. Socket incoming = s.accept( );
19. BufferedReader in = new BufferedReader
20. (new InputStreamReader(incoming.getInputStream()));
21. PrintWriter out = new PrintWriter
22. (incoming.getOutputStream(), true /* autoFlush */);
23.
24. out.println( "Hello! Enter BYE to exit." );
25.
26. // echo client input
27. boolean done = false;
28. while (!done)
29. {
30. String line = in.readLine();
31. if (line == null) done = true;
32. else
33. {
34. out.println("Echo: " + line);
35.
36. if (line.trim().equals("BYE"))
37. done = true;
38. }
39. }
40. incoming.close();
41. }
42. catch (Exception e)
43. {
44. e.printStackTrace();
45. }
46. }
47. }
To try it out, you need to compile and run the program. Then, use telnet to connect to the following server and port:
Server: 127.0.0.1
Port: 8189
The IP address 127.0.0.1 is a special address, called the local loopback address, that denotes the local machine. Since you are running the echo server locally, that is where you want to connect.
NOTE
![]() |
If you are using a dial-up connection, you need to have it running for this experiment. Even though you are only talking to your local machine, the network software must be loaded. |
Actually, anyone in the world can access your echo server, provided it is running and they know your IP address and the magic port number.
When you connect to the port, you will get the message shown in Figure 3-4:

Hello! Enter BYE to exit.
Type anything and watch the input echo on your screen. Type BYE (all uppercase letters) to disconnect. The server program will terminate as well.
There is one problem with the simple server in the preceding example. Suppose we want to allow multiple clients to connect to our server at the same time. Typically, a server runs constantly on a server computer, and clients from all over the Internet may want to use the server at the same time. Rejecting multiple connections allows any one client to monopolize the service by connecting to it for a long time. We can do much better through the magic of threads.
Every time we know the program has established a new socket connection, that is, when the call to accept was successful, we will launch a new thread to take care of the connection between the server and that client. The main program will just go back and wait for the next connection. For this to happen, the main loop of the server should look like this:
while (true)
{
Socket incoming = s.accept();
Thread t = new ThreadedEchoHandler(incoming);
t.start();
}
The ThreadedEchoHandler class derives from Thread and contains the communication loop with the client in its run method.
class ThreadedEchoHandler extends Thread
{ . . .
public void run()
{
try
{
BufferedReader in = new BufferedReader
(new InputStreamReader(incoming.getInputStream()));
PrintWriter out = new PrintWriter
(incoming.getOutputStream(), true /* autoFlush */);
String line;
while ((line = in.readLine()) != null)
{
process line
}
incoming.close();
}
catch(Exception e)
{
handle exception
}
}
}
Because each connection starts a new thread, multiple clients can connect to the server at the same time. You can easily check out this fact. Compile and run the server program (Example 3-3). Open several telnet windows as we have in Figure 3-5. You can communicate through all of them simultaneously. The server program never dies. Use ctrl+c to kill it.

1. import java.io.*;
2. import java.net.*;
3.
4. /**
5. This program implements a multithreaded server that listens to
6. port 8189 and echoes back all client input.
7. */
8. public class ThreadedEchoServer
9. {
10. public static void main(String[] args )
11. {
12. try
13. {
14. int i = 1;
15. ServerSocket s = new ServerSocket(8189);
16.
17. for (;;)
18. {
19. Socket incoming = s.accept( );
20. System.out.println("Spawning " + i);
21. Thread t = new ThreadedEchoHandler(incoming, i);
22. t.start();
23. i++;
24. }
25. }
26. catch (Exception e)
27. {
28. e.printStackTrace();
29. }
30. }
31. }
32.
33. /**
34. This class handles the client input for one server socket
35. connection.
36. */
37. class ThreadedEchoHandler extends Thread
38. {
39. /**
40. Constructs a handler.
41. @param i the incoming socket
42. @param c the counter for the handlers (used in prompts)
43. */
44. public ThreadedEchoHandler(Socket i, int c)
45. {
46. incoming = i; counter = c;
47. }
48.
49. public void run()
50. {
51. try
52. {
53. BufferedReader in = new BufferedReader
54. (new InputStreamReader(incoming.getInputStream()));
55. PrintWriter out = new PrintWriter
56. (incoming.getOutputStream(), true /* autoFlush */);
57.
58. out.println( "Hello! Enter BYE to exit." );
59.
60. boolean done = false;
61. while (!done)
62. {
63. String str = in.readLine();
64. if (str == null) done = true;
65. else
66. {
67. out.println("Echo (" + counter + "): " + str);
68.
69. if (str.trim().equals("BYE"))
70. done = true;
71. }
72. }
73. incoming.close();
74. }
75. catch (Exception e)
76. {
77. e.printStackTrace();
78. }
79. }
80.
81. private Socket incoming;
82. private int counter;
83. }
![]()
ServerSocket(int port) throws IOException
creates a server socket that monitors a port.
| Parameters: | port | the port number |
Socket accept() throws IOException
waits for a connection. This method will block (that is, idle) the current thread until the connection is made. The method returns a Socket object through which the program can communicate with the connecting client.
void close() throws IOException
closes the server socket.
In this section, we show you a practical example of socket programming: a program that sends e-mail to a remote site.
To send e-mail, you make a socket connection to port 25, the SMTP port. SMTP is the Simple Mail Transport Protocol that describes the format for e-mail messages. You can connect to any server that runs an SMTP service. On UNIX machines, that service is typically implemented by the sendmail daemon. However, the server must be willing to accept your request. It used to be that sendmail servers were routinely willing to route e-mail from anyone, but in these days of spam floods, most servers now have built-in checks and only accept requests from users, domains, or IP address ranges that they trust.
Once you are connected to the server, send a mail header (in the SMTP format, which is easy to generate), followed by the mail message.
Here are the details:
Open a socket to your host.
Socket s = new Socket("mail.yourserver.com", 25); // 25 is SMTP
PrintWriter out = new PrintWriter(s.getOutputStream());
Send the following information to the print stream:
HELO sending host MAIL FROM: <sender email address> RCPT TO: <recipient email address> DATA mail message (any number of lines) . QUIT
The SMTP specification (RFC 821) states that lines must be terminated with \r followed by \n.
Most SMTP servers do not check the veracity of the information—you may be able to supply any sender you like. (Keep this in mind the next time you get an e-mail message from president@whitehouse.gov inviting you to a black-tie affair on the front lawn. Anyone could have connected to an SMTP server and created a fake message.)
The program in Example 3-4 is a very simple e-mail program. As you can see in Figure 3-6, you type in the sender, recipient, mail message, and SMTP server. Then, click on the "Send" button, and your message is sent.

The program simply sends the sequence of commands that we just discussed. It displays the commands that it sends to the SMTP server and the responses that it receives. Note that the communication with the mail server occurs in a separate thread so that the user interface thread is not blocked when the program tries to connect to the mail server. (See Chapter 1 for more details on threads in Swing applications.)
1. import java.awt.*;
2. import java.awt.event.*;
3. import java.util.*;
4. import java.net.*;
5. import java.io.*;
6. import javax.swing.*;
7.
8. /**
9. This program shows how to use sockets to send plain text
10. mail messages.
11. */
12. public class MailTest
13. {
14. public static void main(String[] args)
15. {
16. JFrame frame = new MailTestFrame();
17. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
18. frame.show();
19. }
20. }
21.
22. /**
23. The frame for the mail GUI.
24. */
25. class MailTestFrame extends JFrame
26. {
27. public MailTestFrame()
28. {
29. setSize(WIDTH, HEIGHT);
30. setTitle("MailTest");
31.
32. getContentPane().setLayout(new GridBagLayout());
33.
34. GridBagConstraints gbc = new GridBagConstraints();
35. gbc.fill = GridBagConstraints.HORIZONTAL;
36. gbc.weightx = 0;
37. gbc.weighty = 0;
38.
39. gbc.weightx = 0;
40. add(new JLabel("From:"), gbc, 0, 0, 1, 1);
41. gbc.weightx = 100;
42. from = new JTextField(20);
43. add(from, gbc, 1, 0, 1, 1);
44.
45. gbc.weightx = 0;
46. add(new JLabel("To:"), gbc, 0, 1, 1, 1);
47. gbc.weightx = 100;
48. to = new JTextField(20);
49. add(to, gbc, 1, 1, 1, 1);
50.
51. gbc.weightx = 0;
52. add(new JLabel("SMTP server:"), gbc, 0, 2, 1, 1);
53. gbc.weightx = 100;
54. smtpServer = new JTextField(20);
55. add(smtpServer, gbc, 1, 2, 1, 1);
56.
57. gbc.fill = GridBagConstraints.BOTH;
58. gbc.weighty = 100;
59. message = new JTextArea();
60. add(new JScrollPane(message), gbc, 0, 3, 2, 1);
61.
62. communication = new JTextArea();
63. add(new JScrollPane(communication), gbc, 0, 4, 2, 1);
64.
65. gbc.weighty = 0;
66. JButton sendButton = new JButton("Send");
67. sendButton.addActionListener(new
68. ActionListener()
69. {
70. public void actionPerformed(ActionEvent evt)
71. {
72. new
73. Thread()
74. {
75. public void run()
76. {
77. sendMail();
78. }
79. }.start();
80. }
81. });
82. JPanel buttonPanel = new JPanel();
83. buttonPanel.add(sendButton);
84. add(buttonPanel, gbc, 0, 5, 2, 1);
85. }
86.
87. /**
88. Add a component to this frame.
89. @param c the component to add
90. @param gbc the grid bag constraints
91. @param x the grid bax column
92. @param y the grid bag row
93. @param w the number of grid bag columns spanned
94. @param h the number of grid bag rows spanned
95. */
96. private void add(Component c, GridBagConstraints gbc,
97. int x, int y, int w, int h)
98. {
99. gbc.gridx = x;
100. gbc.gridy = y;
101. gbc.gridwidth = w;
102. gbc.gridheight = h;
103. getContentPane().add(c, gbc);
104. }
105.
106. /**
107. Sends the mail message that has been authored in the GUI.
108. */
109. public void sendMail()
110. {
111. try
112. {
113. Socket s = new Socket(smtpServer.getText(), 25);
114.
115. out = new PrintWriter(s.getOutputStream());
116. in = new BufferedReader(new
117. InputStreamReader(s.getInputStream()));
118.
119. String hostName
120. = InetAddress.getLocalHost().getHostName();
121.
122. receive();
123. send("HELO " + hostName);
124. receive();
125. send("MAIL FROM: <" + from.getText() +">");
126. receive();
127. send("RCPT TO: <" + to.getText() +">");
128. receive();
129. send("DATA");
130. receive();
131. StringTokenizer tokenizer = new StringTokenizer(
132. message.getText(), "\n");
133. while (tokenizer.hasMoreTokens())
134. send(tokenizer.nextToken());
135. send(".");
136. receive();
137. s.close();
138. }
139. catch (IOException exception)
140. {
141. communication.append("Error: " + exception);
142. }
143. }
144.
145. /**
146. Sends a string to the socket and echoes it in the
147. communication text area.
148. @param s the string to send.
149. */
150. public void send(String s) throws IOException
151. {
152. communication.append(s);
153. communication.append("\n");
154. out.print(s);
155. out.print("\r\n");
156. out.flush();
157. }
158.
159. /**
160. Receives a string from the socket and displays it
161. in the communication text area.
162. */
163. public void receive() throws IOException
164. {
165. String line = in.readLine();
166. if (line != null)
167. {
168. communication.append(line);
169. communication.append("\n");
170. }
171. }
172.
173. private BufferedReader in;
174. private PrintWriter out;
175. private JTextField from;
176. private JTextField to;
177. private JTextField smtpServer;
178. private JTextArea message;
179. private JTextArea communication;
180.
181. public static final int WIDTH = 300;
182. public static final int HEIGHT = 300;
183. }
In real-life programs, you don't just want to read from a socket, because the read methods will block until data is available. If the host is unreachable, then your application waits for a long time, and you are at the mercy of the underlying operating system to time out eventually.
Instead, you should decide what timeout value is reasonable for your particular application. Then, call the setSoTimeout method to set a timeout value (in milliseconds).
Socket s = new Socket(. . .); s.setSoTimeout(10000); // time out after 10 seconds
If the timeout value has been set for a socket, then all subsequent read operations throw an InterruptedIOException when the timeout has been reached before input is available. You can catch that exception and react to the timeout.
try
{
String line
while ((String line = in.readLine()) != null)
{
process line
}
}
catch (InterruptedIOException exception)
{
react to timeout
}
There is one additional timeout issue that you need to address. The constructor
Socket(String host, int port)
can block indefinitely until an initial connection to the host is established.
As of SDK 1.4, you can overcome this problem by first constructing an unconnected socket and then connecting it with a timeout:
Socket s = new Socket(); s.connect(new InetSocketAddress(host, port), timeout);
TIP
![]() |
If you use an older version of the SDK, you need to construct the socket in a separate thread and wait for that thread to either complete or time out. Socket socket = null;
Thread t = new Thread()
{
public void run()
{
try
{
socket = new Socket(host, port);
}
catch (IOException exception)
{
}
}
};
t.start();
try
{
t.join(timeout);
}
catch (InterruptedException exception)
{
}
The join method returns when the thread has completed or when the timeout has expired, whichever happens first. If the socket variable is not null, then the socket has been successfully opened within the allotted time. |
When a client program sends a request to the server, the server needs to be able to determine when the end of the request occurs. For that reason, many Internet protocols (such as SMTP) are line-oriented. Other protocols contain a header that specifies the size of the request data. Otherwise, indicating the end of the request data is harder than writing data to a file. With a file, you'd just close the file at the end of the data. But if you close a socket, then you immediately disconnect from the server.
The half-close overcomes this problem. You can close the output stream of a socket, thereby indicating to the server the end of the request data, but keep the input stream open so that you can read the response.
The client side looks like this:
Socket socket = new Socket(host, port); BufferedReader reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); PrintWriter writer = new PrintWriter( socket.getOutputStream()); // send request data writer.print(. . .); writer.flush(); socket.shutdownOutput(); // now socket is half closed // read response data String line; while ((line = reader.readLine()) != null) . . . socket.close();
The server side simply reads input until the end of the input stream is reached.
Of course, this protocol is only useful for one-shot services such as HTTP where the client connects, issues a request, catches the response, and then disconnects.
Usually, you don't have to worry too much about Internet addresses—the numerical host addresses that consist of four bytes (or, with IPv6, six bytes) such as 132.163.4.102. However, you can use the InetAddress class if you need to convert between host names and Internet addresses.
As of SDK 1.4, the java.net package supports IPv6 Internet addresses, provided the host operating system does. For example, you'll be able to make use of IPv6 addresses on Solaris but, at the time of this writing, not on Windows.
The static getByName method returns an InetAddress object of a host.
For example,
InetAddress address
= InetAddress.getByName("time-A.timefreq.bldrdoc.gov");
returns an InetAddress object that encapsulates the sequence of four bytes 132.163.4.102. You can access the bytes with the getAddress method.
byte[] addressBytes = address.getAddress();
Some host names with a lot of traffic correspond to multiple Internet addresses, to facilitate load balancing. For example, at the time of this writing, the host name java.sun.com corresponds to three different Internet addresses. One of them is picked at random when the host is accessed. You can get all hosts with the getAllByName method.
InetAddress[] addresses = InetAddress.getAllByName(host);
Finally, you sometimes need the address of the local host. If you simply ask for the address of localhost, you always get the address 127.0.0.1, which isn't very useful. Instead, use the static getLocalHost method to get the address of your local host.
InetAddress address = InetAddress.getLocalHost();
Example 3-5 is a simple program that prints the Internet address of your local host if you do not specify any command-line parameters, or all Internet addresses of another host if you specify the host name on the command line, such as
java InetAddressTest java.sun.com
1. import java.net.*;
2.
3. /**
4. This program demonstrates the InetAddress class.
5. Supply a host name as command line argument, or run
6. without command line arguments to see the address of the
7. local host.
8. */
9. public class InetAddressTest
10. {
11. public static void main(String[] args)
12. {
13. try
14. {
15. if (args.length > 0)
16. {
17. String host = args[0];
18. InetAddress[] addresses
19. = InetAddress.getAllByName(host);
20. for (int i = 0; i < addresses.length; i++)
21. System.out.println(addresses[i]);
22. }
23. else
24. {
25. InetAddress localHostAddress
26. = InetAddress.getLocalHost();
27. System.out.println(localHostAddress);
28. }
29. }
30. catch (Exception e)
31. {
32. e.printStackTrace();
33. }
34. }
35. }
NOTE
![]() |
In this book, we cover only the TCP (Transmission Control Protocol) networking protocol. TCP establishes a reliable connection between two computers. The Java platform also supports the so-called UDP (User Datagram Protocol) protocol, which can be used to send packets (also called datagrams) with much less overhead than that for TCP. The drawback is that the packets can be delivered in random order, or even dropped altogether. It is up to the recipient to put the packets in order and to request retransmission of missing packets. UDP is most suited for applications where missing packets can be tolerated, for example, in audio or video streams, or for continuous measurements. |
![]()
Socket(String host, int port)
creates a socket and connects it to a port on a remote host.
| Parameters: | host | the host name |
| port | the port number |
Socket()
creates a socket that has not yet been connected.
void connect(SocketAddress address)
connects this socket to the given address. (Since SDK 1.4)
void connect(SocketAddress address, int timeout)
connects this socket to the given address or returns if the time interval expired. (Since SDK 1.4)
| Parameters: | address | the remote address |
| timeout | the timeout in milliseconds |
boolean isConnected()
returns true if the socket is connected. (Since SDK 1.4)
void close()
closes the socket.
boolean isClosed()
returns true if the socket is closed. (Since SDK 1.4)
InputStream getInputStream()
gets the input stream to read from the socket.
OutputStream getOutputStream()
gets an output stream to write to this socket.
void setSoTimeout(int timeout)
sets the blocking time for read requests on this Socket. If the timeout is reached, then an InterruptedIOException is raised.
| Parameters: | timeout | the timeout in milliseconds (0 for infinite timeout) |
void shutdownOutput()
sets the output stream to "end of stream."
void shutdownInput()
sets the input stream to "end of stream."
boolean isOutputShutdown
returns true if output has been shut down. (Since SDK 1.4)
boolean isInputShutdown
returns true if input has been shut down.(Since SDK 1.4)
![]()
static InetAddress getByName(String host)
static InetAddress[] getAllByName(String host)
These methods construct an InetAddress, or an array of all Internet addresses, for the given host name.
static InetAddress getLocalHost()
constructs an InetAddress for the local host.
byte[] getAddress()
returns an array of bytes that contains the numerical address.
String getHostAddress()
returns a string with decimal numbers, separated by periods, for example "132.163.4.102".
String getHostName()
returns the host name.
![]()
Since SDK 1.4:
InetSocketAddress(String hostname, int port)
constructs an address object with the given host and port, resolving the host name during construction. If the host name cannot be resolved, then the address object's unresolved property is set to true.
boolean isUnresolved()
returns true if this address object could not be resolved.
In the last section, you saw how to use socket-level programming to connect to an SMTP server and send an e-mail message. It is nice to know that this can be done, and to get a glimpse of what goes on "under the hood" of an Internet service such as e-mail. However, if you are planning an application that incorporates e-mail, you will probably want to work at a higher level and use a library that encapsulates the protocol details. For example, Sun Microsystems has developed the JavaMail API as a standard extension of the Java platform. In the JavaMail API, you simply issue a call such as
Transport.send(message);
to send a message. The library takes care of message protocols, multiple recipients, handling attachments, and so on.
For the remainder of this chapter, we will concentrate on higher-level services that the standard edition of the Java platform provides. Of course, the runtime library uses sockets to implement these services. But you don't have to worry about the protocol details when you use the higher-level services.
The URL and URLConnection classes encapsulate much of the complexity of retrieving information from a remote site. Here is how you specify a URL.
URL url = new URL(urlString);
The Java 2 platform supports both HTTP and FTP resources.
If you simply want to fetch the contents of the resource, then you can use the openStream method of the URL class. This method yields an InputStream object. Using this stream object, you can easily read the contents of the resource.
InputStream uin = url.openStream(); BufferedReader in = new BufferedReader(new InputStreamReader(uin)); String line; while ((line = in.readLine()) != null) { process line; }
As of SDK 1.4, the java.net package makes a useful distinction between URLs (uniform resource locators) and URIs (uniform resource identifiers).
A URI is a purely syntactical construct that specifies the various parts of the string specifying a web resource. A URL is a special kind of URI, namely one with sufficient information to locate a resource. Other URIs, such as
mailto:cay@horstmann.com
are not locators—there is no data to locate from this identifier. Such a URI is called a URN (uniform resource name).
In the Java library, the URI class has no methods for accessing the resource that the identifier specifies—its sole purpose is parsing. In contrast, the URL class can open a stream to the resource. For that reason, the URL class only works with identifiers of a kind that the Java library knows how to handle, such as an HTTP or FTP URL.
To see why a URI class is necessary, consider how complex URLs can be. For example,
http://maps.yahoo.com/py/maps.py?csz=Cupertino+CA ftp://username:password@ftp.yourserver.com/pub/file.txt
The URI specification gives rules for the makeup of these identifiers. A URI has the syntax
[scheme:]schemeSpecificPart[#fragment]
Here, the [ … ] denotes an optional part, and the: and # are included literally in the identifier.
If the scheme: part is present, the URI is called absolute. Otherwise, it is called relative.
An absolute URI is opaque if the schemeSpecificPart does not begin with a / such as
mailto:cay@horstmann.com
All absolute non-opaque URIs and all relative URIs are hierarchical. Examples are:
http://java.sun.com/index.html ../../java/net/Socket.html#Socket()
The schemeSpecificPart of a hierarchical URI has the structure
[//authority][path][?query]
where again [ … ] denotes optional parts.
For server-based URIs, the authority part has the form
[user-info@]host[:port]
The port must be an integer.
RFC 2396, which standardizes URIs, also supports a registry-based mechanism where the authority has a different format, but they are not in common use.
One of the purposes of the URI class is to parse an identifier and break it up into its various components. You can retrieve them with the methods
getScheme getSchemeSpecificPart getAuthority getUserInfo getHost getPort getPath getQuery getFragment
The other purpose of the URI class is the handling of absolute and relative identifiers. If you have an absolute URI such as
http://docs.mycompany.com/api/java/net/ServerSocket.html
and a relative URI such as
../../java/net/Socket.html#Socket()
then you can combine the two into an absolute URI.
http://docs.mycompany.com/api/java/net/Socket.html#Socket()
This process is called resolving a relative URL.
The opposite process is called relativization. For example, suppose you have a base URI
http://docs.mycompany.com/api
and a URI
http://docs.mycompany.com/api/java/lang/String.html
then the relativized URI is
java/lang/String.html
The URI class supports both of these operations:
relative = base.relativize(combined); combined = base.resolve(relative);
If you want additional information about a web resource, then you should use the URLConnection class, which gives you much more control than the basic URL class.
When working with a URLConnection object, you must carefully schedule your steps, as follows:
Call the openConnection method of the URL class to obtain the URLConnection object:
URLConnection connection = url.openConnection();
Set any request properties, using the methods
setDoInput setDoOutput setIfModifiedSince setUseCaches setAllowUserInteraction setRequestProperty
We discuss these methods later in this section and in the API notes.
Connect to the remote resource by calling the connect method.
connection.connect();
Besides making a socket connection to the server, this method also queries the server for header information.
After connecting to the server, you can query the header information. There are two methods, getHeaderFieldKey and getHeaderField, to enumerate all fields of the header. As of SDK 1.4, there is also a method getHeaderFields that gets a standard Map object containing the header fields. For your convenience, the following methods query standard fields.
getContentType getContentLength getContentEncoding getDate getExpiration getLastModified
Finally, you can access the resource data. Use the getInputStream method to obtain an input stream for reading the information. (This is the same input stream that the openStream method of the URL class returns.) There is also a method getObject; however, in practice, it isn't very useful. The objects that are returned by standard content types such as text/plain and image/gif require classes in the com.sun hierarchy for processing. It is possible to register your own content handlers, but we will not discuss that technique in this book.
CAUTION
![]() |
Some programmers form the wrong mental image when using the URLConnection class and think that the getInputStream and getOutputStream methods are similar to those of the Socket class. But that isn't quite true. The URLConnection class does quite a bit of magic behind the scenes, in particular the handling of request and response headers. For that reason, it is important that you follow the setup steps for the connection. |
Let us now look at some of the URLConnection methods in detail. There are several methods to set properties of the connection before connecting to the server. The most important ones are setDoInput and setDoOutput. By default, the connection yields an input stream for reading from the server but no output stream for writing. If you want an output stream (for example, for posting data to a web server—see the next section), then you need to call
connection.setDoOutput(true);
Next, you may want to set some of the request headers. The request headers are sent together with the request command to the server. Here is an example:
GET www.server.com/index.html HTTP/1.0 Referer: http://www.somewhere.com/links.html Proxy-Connection: Keep-Alive User-Agent: Mozilla/4.76 (Windows ME; U) Opera 5.11 [en] Host: www.server.com Accept: text/html, image/gif, image/jpeg, image/png, */* Accept-Language: en Accept-Charset: iso-8859-1,*,utf-8 Cookie: orangemilano=192218887821987
The setIfModifiedSince method tells the connection that you are only interested in data that has been modified since a certain date. The setUseCaches and setAllowUserInteraction are only used inside applets. The setUseCaches method directs the ambient browser to first check the browser cache. The setAllowUserInteraction method allows an application to pop up a dialog for querying the user name and password for password-protected resources (see Figure 3-7). These settings have no effect outside of applets.

Finally, there is a catch-all setRequestProperty method that you can use to set any name/value pair that is meaningful for the particular protocol. For the format of the HTTP request headers, see RFC 2616. Some of these parameters are not well documented and are passed around by word of mouth from one programmer to the next. For example, if you want to access a password-protected web page, you must do the following:
Concatenate the user name, a colon, and the password.
String input = username + ":" + password;
Compute the Base 64 encoding of the resulting string. (The Base64 encoding encodes a sequence of bytes into a sequence of printable ASCII characters.)
String encoding = base64Encode(input);
Call the setRequestProperty method with a name of "Authorization" and value "Basic " + encoding:
connection.setRequestProperty("Authorization",
"Basic " + encoding);
TIP
![]() |
You just saw how to access a password-protected web page. To access a password-protected file by FTP, you use an entirely different method. You simply construct a URL of the form ftp://username:password@ftp.yourserver.com/pub/file.txt This, too, does not seem to be well documented. We found out by looking inside rt.jar, locating the promising-looking class sun.net.www.protocol.ftp.FtpURLConnection, and decompiling that class with javap. |
Once you call the connect method, you can query the response header information. First, let us see how to enumerate all response header fields. The implementors of this class felt a need to express their individuality by introducing yet another iteration protocol. The call
String key = connection.getHeaderFieldKey(n);
gets the nth key from the response header, where n starts from 1! It returns null if n is zero or larger than the total number of header fields. There is no method to return the number of fields; you simply keep calling getHeaderFieldKey until you get null. Similarly, the call
String value = connection.getHeaderField(n);
returns the nth value.
Mercifully, as of SDK 1.4, there is a method getHeaderFields that returns a Map of response header fields that you can access as explained in Chapter 2.
Map headerFields = connection.getHeaderFields();
Here is a set of response header fields from a typical HTTP request.
Date: Wed, 29 Aug 2001 00:15:48 GMT Server: Apache/1.3.3 (Unix) Last-Modified: Sun, 24 Jun 2001 20:53:38 GMT ETag: "28094e-12cd-37729ad2" Accept-Ranges: bytes Content-Length: 4813 Connection: close Content-Type: text/html
As a convenience, six methods query the values of the most common header types and convert them to numeric types when appropriate. Table 3-1 shows these convenience methods. The methods with return type long return the number of seconds since January 1, 1970 GMT.
| Key name | Method name | Return type |
|---|---|---|
| Date | getDate | long |
| Expires | getExpiration | long |
| Last-Modified | getLastModified | long |
| Content-Length | getContentLength | int |
| Content-Type | getContentType | String |
| Content-Encoding | getContentEncoding | String |
The program in Example 3-6 lets you experiment with URL connections. Supply a URL and an optional user name and password on the command line when running the program, for example:
java URLConnectionTest http://www.yourserver.com user pw
The program prints out
All keys and values of the header;
The return values of the six convenience methods in Table 3-1;
The first ten lines of the requested resource.
The program is straightforward, except for the computation of the Base 64 encoding. There is an undocumented class, sun.misc.Base64Encoder, that you can use instead of the one that we provide in the example program. Simply replace the call to base64Encode with
String encoding = new sun.misc.BASE64Encoder().encode(input.getBytes());
However, we supplied our own class because we do not like to rely on the classes in the sun or com.sun packages.
NOTE
![]() |
The javax.mail.internet.MimeUtility class in the JavaMail standard extension package also has a method for Base64 encoding. SDK 1.4 includes a package containing the visible (non-public) class java.util.prefs.Base64. |
1. import java.io.*;
2. import java.net.*;
3. import java.util.*;
4.
5. /**
6. This program connects to an URL and displays the
7. response header data and the first 10 lines of the
8. requested data.
9.
10. Supply the URL and an optional username and password (for
11. HTTP basic authentication) on the command line.
12. */
13. public class URLConnectionTest
14. {
15. public static void main(String[] args)
16. {
17. try
18. {
19. String urlName;
20. if (args.length > 0)
21. urlName = args[0];
22. else
23. urlName = "http://java.sun.com";
24.
25. URL url = new URL(urlName);
26. URLConnection connection = url.openConnection();
27.
28. // set username, password if specified on command line
29.
30. if (args.length > 2)
31. {
32. String username = args[1];
33. String password = args[2];
34. String input = username + ":" + password;
35. String encoding = base64Encode(input);
36. connection.setRequestProperty("Authorization",
37. "Basic " + encoding);
38. }
39.
40. connection.connect();
41.
42. // print header fields
43.
44. int n = 1;
45. String key;
46. while ((key = connection.getHeaderFieldKey(n)) != null)
47. {
48. String value = connection.getHeaderField(n);
49. System.out.println(key + ": " + value);
50. n++;
51. }
52.
53. // print convenience functions
54.
55. System.out.println("----------");
56. System.out.println("getContentType: "
57. + connection.getContentType());
58. System.out.println("getContentLength: "
59. + connection.getContentLength());
60. System.out.println("getContentEncoding: "
61. + connection.getContentEncoding());
62. System.out.println("getDate: "
63. + connection.getDate());
64. System.out.println("getExpiration: "
65. + connection.getExpiration());
66. System.out.println("getLastModifed: "
67. + connection.getLastModified());
68. System.out.println("----------");
69.
70.
71. BufferedReader in = new BufferedReader(new
72. InputStreamReader(connection.getInputStream()));
73.
74. // print first ten lines of contents
75.
76. String line;
77. n = 1;
78. while ((line = in.readLine()) != null && n <= 10)
79. {
80. System.out.println(line);
81. n++;
82. }
83. if (line != null) System.out.println(". . .");
84. }
85. catch (IOException exception)
86. {
87. exception.printStackTrace();
88. }
89. }
90.
91. /**
92. Computes the Base64 encoding of a string
93. @param s a string
94. @return the Base 64 encoding of s
95. */
96. public static String base64Encode(String s)
97. {
98. ByteArrayOutputStream bOut
99. = new ByteArrayOutputStream();
100. Base64OutputStream out = new Base64OutputStream(bOut);
101. try
102. {
103. out.write(s.getBytes());
104. out.flush();
105. }
106. catch (IOException exception)
107. {
108. }
109. return bOut.toString();
110. }
111. }
112.
113. /**
114. This stream filter converts a stream of bytes to their
115. Base64 encoding.
116.
117. Base64 encoding encodes 3 bytes into 4 characters.
118. |11111122|22223333|33444444|
119. Each set of 6 bits is encoded according to the
120. toBase64 map. If the number of input bytes is not
121. a multiple of 3, then the last group of 4 characters
122. is padded with one or two = signs. Each output line
123. is at most 76 characters.
124. */
125. class Base64OutputStream extends FilterOutputStream
126. {
127. /**
128. Constructs the stream filter
129. @param out the stream to filter
130. */
131. public Base64OutputStream(OutputStream out)
132. {
133. super(out);
134. }
135.
136. public void write(int c) throws IOException
137. {
138. inbuf[i] = c;
139. i++;
140. if (i == 3)
141. {
142. super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
143. super.write(toBase64[((inbuf[0] & 0x03) << 4) |
144. ((inbuf[1] & 0xF0) >> 4)]);
145. super.write(toBase64[((inbuf[1] & 0x0F) << 2) |
146. ((inbuf[2] & 0xC0) >> 6)]);
147. super.write(toBase64[inbuf[2] & 0x3F]);
148. col += 4;
149. i = 0;
150. if (col >= 76)
151. {
152. super.write('\n');
153. col = 0;
154. }
155. }
156. }
157.
158. public void flush() throws IOException
159. {
160. if (i == 1)
161. {
162. super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
163. super.write(toBase64[(inbuf[0] & 0x03) << 4]);
164. super.write('=');
165. super.write('=');
166. }
167. else if (i == 2)
168. {
169. super.write(toBase64[(inbuf[0] & 0xFC) >> 2]);
170. super.write(toBase64[((inbuf[0] & 0x03) << 4) |
171. ((inbuf[1] & 0xF0) >> 4)]);
172. super.write(toBase64[(inbuf[1] & 0x0F) << 2]);
173. super.write('=');
174. }
175. }
176.
177. private static char[] toBase64 =
178. {
179. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
180. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
181. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
182. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
183. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
184. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
185. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
186. '4', '5', '6', '7', '8', '9', '+', '/'
187. };
188.
189. private int col = 0;
190. private int i = 0;
191. private int[] inbuf = new int[3];
192. }
NOTE
![]() |
A commonly asked question is whether the Java platform supports access of secure web pages. If you open a connection to an https URL inside an applet, then the applet will indeed access the page, by taking advantage of the SSL implementation of the browser. However, before SDK 1.4, there was no support for https URLs. For patent and export restrictions, you need to install a separate extension library and a cryptographic service provider for the RSA algorithm. As of SDK 1.4, SSL support is a part of the standard library. |
![]()
InputStream openStream()
opens an input stream for reading the resource data.
URLConnection openConnection();
returns a URLConnection object that manages the connection to the resource.
![]()
void setDoInput(boolean doInput)
If doInput is true, then the user can receive input from this URLConnection.
void setDoOutput(boolean doOutput)
If doOutput is true, then the user can send output to this URLConnection.
void setIfModifiedSince(long time)
configures this URLConnection to fetch only data that has been modified since a given time. The time is given in seconds from midnight, GMT, January 1, 1970.
void setUseCaches(boolean useCaches)
If useCaches is true, then data can be retrieved from a local cache. Note that the URLConnection itself does not maintain such a cache. The cache must be supplied by an external program such as a browser.
void setAllowUserInteraction(boolean allowUserInteraction)
If allowUserInteraction is true, then the user can be queried for passwords. Note that the URLConnection itself has no facilities for executing such a query. The query must be carried out by an external program such as a browser or browser plug-in.
void setRequestProperty(String key, String value)
sets a request header field.
Map getRequestProperties()
returns a map of request header key/value pairs. This method was added in SDK 1.4.
void connect()
connects to the remote resource and retrieves response header information.
Map getHeaderFields()
returns a map of response header field name/value pairs. This method was added in SDK 1.4.
String getHeaderFieldKey(int n)
gets the nth response header field key, or
null if n is
0 or larger than the
number of response header fields.
String getHeaderField(int n)
gets the nth response header field value, or
null if n is
0 or larger than the
number of response header fields.
int getContentLength()
gets the content length, if available, or -1 if unknown.
String getContentType
gets the content type, such as text/plain or image/gif.
String getContentEncoding()
gets the content encoding, such as gzip. This value is not commonly used, as the default identity encoding is not supposed to be specified with a Content-Encoding header.
long getDate()
long getExpiration()
long getLastModifed()
These methods get the date of creation, expiration, and last modification of the resource. The dates are specified as seconds from midnight, GMT, January 1, 1970.
InputStream openInputStream()
OutputStream openOutputStream()
These methods return a stream for reading from the resource or writing to the resource.
Object getObject()
selects the appropriate content handler to read the resource data and convert it into an object. This method is not useful for reading standard types such as text/plain or image/gif unless you install your own content handler.
In the preceding section, you saw how to read data from a web server. Now we will show you how your programs can send data back to a web server and to programs that the web server invokes, using the CGI and servlet mechanisms.
Even before Java technology came along, there was a mechanism for writing interactive web applications. To send information from a web browser to the web server, a user would fill out a form, like the one in Figure 3-8.

When the user clicks on the "Submit" button, the text in the text fields and the settings of the check boxes and radio buttons are sent back to the server to be processed by a CGI script. (CGI stands for Common Gateway Interface.) The CGI script to use is specified in the ACTION attribute of the FORM tag.
The CGI script is a program that resides on the server computer. There are usually many CGI scripts on a server, conventionally residing in the cgi-bin directory. The web server launches the CGI script and feeds it the form data.
The CGI script processes the form data and produces another HTML page that the web server sends back to the browser. This sequence is illustrated in Figure 3-9. The response page can contain new information (for example, in an information-search program) or just an acknowledgment. The web browser then displays the response page.

CGI programs are commonly written in Perl, but they can be written in any language that can read from standard input and write to standard output.
NOTE
![]() |
We will not discuss how to design HTML forms that interact with CGI. A good reference for that topic is HTML & XHTML: The Definitive Guide (4th edition) by Chuck Musciano and Bill Kennedy [O'Reilly, 2000]. Our interest lies in the interface between CGI and Java programs, not in HTML forms. |
CGI is often a good mechanism to use because it is well established and system administrators are familiar with it. It does have a major disadvantage. Each request forks a new process and not just a new thread. Furthermore, it is difficult to control the security of those scripts. The newer servlet technology overcomes both of these disadvantages. Servlet engines use Java technology to start each servlet in a separate thread and to control the security privileges of servlets. You will see an example servlet later in this chapter. For more information on servlets, we recommend the book Core Java Web Server by Chris Taylor and Tim Kimmet [Prentice-Hall, 1998].
When data is sent to a web server, it does not matter whether the data is interpreted by a CGI script or a servlet. The client sends the data to the web server in a standard format, and the web server takes care of passing it on to the program that generates the response.
There are two methods with which to send information to a web server: the GET method and the POST method.
In the GET method, you simply attach parameters to the end of the URL. The URL has the form
http://host/script?parameters
For example, at the time of this writing, the Yahoo web site has a script py/maps.py at the host maps.yahoo.com. The script requires two parameters, addr and csz. You need to separate the parameters by an & and encode the parameters, using the following scheme.
Replace all spaces with a +. Replace all nonalphanumeric characters by a %, followed by a two-digit hexadecimal number. For example, to transmit the street name S. Main, you use S%2e+Main, since the hexadecimal number 2e (or decimal 46) is the ASCII code of the "." character.
This encoding keeps any intermediate programs from messing with spaces and interpreting other special characters. This encoding scheme is called URL encoding.
For example, to get a map of 1 Infinite Loop, Cupertino, CA, simply request the following URL:
http://maps.yahoo.com/py/maps.py?addr=1+Infinite+Loop&csz= Cupertino+CA
The GET method is simple, but it has a major limitation that makes it relatively unpopular. Most browsers have a limit on the number of characters that you can include in a GET request.
In the POST method, you do not attach parameters to a URL. Instead, you get an output stream from the URLConnection and write name/value pairs to the output stream. You still have to URL-encode the values and separate them with & characters.
Let us look at this process in more detail. To post data to a script, you first establish a URLConnection.
URL url = new URL("http://host/script");
URLConnection connection = url.openConnection();
Then, you call the setDoOutput method to set up the connection for output.
connection.setDoOutput(true);
Next, you call getOutputStream to get a stream through which you can send data to the server. If you are sending text to the server, it is convenient to wrap that stream into a PrintWriter.
PrintWriter out = new PrintWriter(connection.getOutputStream());
Now you are ready to send data to the server:
out.print(name1 + "=" + URLEncoder.encode(value1) + "&"); out.print(name2 + "=" + URLEncoder.encode(value2));
Finally, close the output stream.
out.close();
You read the server response in the usual way.
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null)
{
process line
}
Let us run through a practical example. The web site at http://www.census.gov/ipc/www/idbprint.html contains a form to request population data (see Figure 3-10). If you look at the HTML source, you will see the following HTML tag:

<FORM METHOD=POST ACTION="/cgi-bin/ipc/idbsprd">
This tag indicates that the name of the script that is executed when the user clicks the "Submit" button is /cgi-bin/ipc/idbsprd, and that you need to use the POST method to send data to the script.
Next, you need to find out the field names that the script expects. Look at the user interface components. Each of them has a NAME attribute, for example,
<SELECT NAME="tbl" SIZE=8>
<OPTION VALUE="001">001 Total Midyear Population
more options . . .
</SELECT>
This tells you that the name of the field is tbl. This field specifies the population table type. If you specify the table type 001, you will get a table of the total midyear population. If you look further, you will also find a country field name cty with values such as US for the United States and CH (!) for China. (Sadly, the Census Bureau seems to be unaware of the ISO-3166 standard for country codes.)
Finally, there is a field named optyr for the year range selection. For this example, we will just set it to latest checked. Then, the remaining fields will not be filled and the latest available data will be displayed.
For example, to get the latest data for the total midyear population of China, you construct this string:
tbl=1&cty=CH&optyr=latest+checked
Send the string to the URL
The script sends back the following reply:
<PRE>
U.S. Bureau of the Census, International Data Base
Table 001. Total Midyear Population
--------------------------
CtYear Population
--------------------------
CH1999 1246871951
--------------------------
Source: U.S. Bureau of the Census, International
Data Base.
</PRE>
As you can see, the script formats the reply as a minimally acceptable HTML page, by placing <PRE> and </PRE> tags around the data. This particular script doesn't bother with constructing a pretty table, though. That is the reason we picked it as an example—it is easy to see what happens with this script, whereas it can be confusing to decipher a complex set of HTML tags that other scripts produce.
The program in Example 3-7 sends POST data to an arbitrary URL. You can use it to send data to the Census Bureau script that we just discussed or to any other URL. You specify the data in a properties file (by default PostTest.properties), for example,
URL=http://www.census.gov/cgi-bin/ipc/idbsprd tbl=001 cty=CH optyr=latest checked
Here is how the program works. First, we check if the user supplied a command-line argument to override the default property file. Then, we read the property file.
Properties props = new Properties(); FileInputStream in = new FileInputStream(fileName); props.load(in);
Next, we use the URL property to construct the URL object, and we remove that property so that it isn't sent to the web server.
URL url = new URL(props.getProperty("URL"));
props.remove("URL");
In the doPost method, we first open the connection, call doOutput(true), and open the output stream. Then, we enumerate the names and values in the Properties object. For each of them, we send the name, = character, value, and & separator character:
out.print(name);
out.print('=');
out.print(URLEncoder.encode(value));
if (more pairs) out.print('&');
Finally, we read the response from the server.
There is one twist with reading the response. If a script error occurs, then the call to connection.getInputStream() throws a FileNotFoundException. However, the server still sends an error page back to the browser (such as the ubiquitous "Error 404 -page not found"). To capture this error page, you cast the URLConnection object to the HttpURLConnection class and call its getErrorStream method:
InputStream err = ((HttpURLConnection)connection).getErrorStream();
Then, you can read the error page from the stream err.
If you run the program with the supplied properties file, then you will see the result from the Census Bureau script. We supply a second file, Zip.properties, that performs a zip code lookup, using a script provided by the United States Postal Service. The query result contains a list of city names for a given zip code.
The technique that this program displays is useful whenever you need to query information from an existing web site. Simply find out the parameters that you need to send (usually by inspecting the HTML source of a web page that carries out the same query), and then strip out the HTML tags and other unnecessary information from the reply.
1. import java.io.*;
2. import java.net.*;
3. import java.util.*;
4.
5. /**
6. This program demonstrates how to use the URLConnection class
7. for a POST request. The URL and form data are in a .properties
8. file whose name should be supplied on the command line.
9. */
10. public class PostTest
11. {
12. public static void main(String[] args)
13. {
14. try
15. {
16. String fileName;
17. if (args.length > 0)
18. fileName = args[0];
19. else
20. fileName = "PostTest.properties";
21. Properties props = new Properties();
22. FileInputStream in = new FileInputStream(fileName);
23. props.load(in);
24.
25. URL url = new URL(props.getProperty("URL"));
26. props.remove("URL");
27. String r = doPost(url, props);
28. System.out.println(r);
29. }
30. catch (IOException exception)
31. {
32. exception.printStackTrace();
33. }
34. }
35.
36. /**
37. Makes a POST request and returns the server response.
38. @param url the URL to post to
39. @param nameValuePairs a table of name/value pairs to
40. supply in the request.
41. @return the server reply (either from the input stream
42. or the error stream)
43. */
44. public static String doPost(URL url,
45. Properties nameValuePairs) throws IOException
46. {
47. URLConnection connection = url.openConnection();
48. connection.setDoOutput(true);
49.
50. PrintWriter out
51. = new PrintWriter(connection.getOutputStream());
52.
53. Enumeration enum = nameValuePairs.keys();
54.
55. while (enum.hasMoreElements())
56. {
57. String name = (String)enum.nextElement();
58. String value = nameValuePairs.getProperty(name);
59. out.print(name);
60. out.print('=');
61. out.print(URLEncoder.encode(value));
62. if (enum.hasMoreElements()) out.print('&');
63. }
64.
65. out.close();
66.
67. BufferedReader in;
68. StringBuffer response = new StringBuffer();
69. try
70. {
71. in = new BufferedReader(new
72. InputStreamReader(connection.getInputStream()));
73. }
74. catch (IOException e)
75. {
76. if (!(connection instanceof HttpURLConnection)) throw e;
77. InputStream err
78. = ((HttpURLConnection)connection).getErrorStream();
79. if (err == null) throw e;
80. in = new BufferedReader(new InputStreamReader(err));
81. }
82. String line;
83.
84. while ((line = in.readLine()) != null)
85. response.append(line + "\n");
86.
87. in.close();
88. return response.toString();
89. }
90. }
Our example program uses the URLConnection class to post data to a web site. More for curiosity's sake than for practical usage, you may like to know exactly what information the URLConnection sends to the server in addition to the data that you supply.
The URLConnection object first sends a request header to the server. The first line of the header must be
Content-Type: type
where type is usually one of the following:
text/plain text/html application/octet-stream application/x-www-form-urlencoded
The content type must be followed by the line
Content-Length: length
for example,
Content-Length: 1024
The end of the header is indicated by a blank line. Then, the data portion follows. The web server strips off the header and routes the data portion to the server script.
Note that the URLConnection object buffers all data that you send to the output stream since it must first determine the total content length.
![]()
InputStream getErrorStream()
returns a stream from which you can read web server error messages.
![]()
static String encode(String s)
returns the URL-encoded form of the string s. In URL encoding, the characters 'A' - 'Z', 'a'- 'z', '0'- '9', '-', '_', '.' and '*'are left unchanged. Space is encoded into '+', and all other characters are encoded into "%UV", where 0xUV is the lower order byte of the character.
![]()
static string decode(String s)
returns the decoding of the URL encoded string s.
The last example showed you how to read data from a web server. The Internet contains a wealth of information both interesting and not: it is the lack of guidance through this mass of information that is the major complaint of most web users. One promise of the Java technology is that it may help to bring order to this chaos: you can use Java applets to retrieve information and present it to the user in an appealing format.
There are many possible uses. Here are a few that come to mind:
An applet can look at all the web pages the user has specified as interesting and find which have recently changed.
An applet can visit the web pages of all scheduled airlines to find out which is running a special.
Applets can gather and display recent stock quotes, monetary exchange rates, and other financial information.
Applets can search FAQs, press releases, articles, and so on, and return text that contains certain keywords.
We give a simple example of such an applet. The National Weather Service stores weather reports in various files on the server http://iwin.nws.noaa.gov. For example, an hourly forecast for California is stored at
Other directories contain similar reports.
Our applet presents the user with two lists, one with states and another with report types. If you click on Get report, the applet fetches the report and places it into a text area (see Figure 3-11). You will find the code for the applet in Example 3-8.

The applet code itself is straightforward. The only interesting action occurs in the getWeather method. That method first builds up the query string. It obtains the base URL (http://iwin.nws.noaa.gov/iwin/) from the queryBase parameter of the applet tag. Then, it adds the state and report name and the extension .html:
String queryBase = getParameter("queryBase");
String query = queryBase + state + "/" + report + ".html";
Next, we create an URL object and call openStream.
URL url = new URL(query);
BufferedReader in = new BufferedReader(new
InputStreamReader(url.openStream()));
The remainder of the method simply reads the file, removes HTML tags, and places the file in the text area.
String line; while ((line = in.readLine()) != null) weather.append(removeTags(line) + "\n");
1. import java.net.*;
2. import java.io.*;
3. import java.util.*;
4. import java.awt.*;
5. import java.awt.event.*;
6. import java.applet.*;
7. import javax.swing.*;
8.
9. /**
10. This applet displays weather data from a NOAA server.
11. The data is mostly in text format, so it can be displayed
12. in a text area.
13. */
14. public class WeatherApplet extends JApplet
15. {
16. public void init()
17. {
18. Container contentPane = getContentPane();
19. contentPane.setLayout(new BorderLayout());
20.
21. // Set up the lists of choices for states and reports
22. JPanel comboPanel = new JPanel();
23. state = makeCombo(states, comboPanel);
24. report = makeCombo(reports, comboPanel);
25. contentPane.add(comboPanel, BorderLayout.NORTH);
26.
27. // Add the text area
28. weather = new JTextArea(20, 80);
29. weather.setFont(new Font("Courier", Font.PLAIN, 12));
30.
31. // Add the report button
32. contentPane.add(new JScrollPane(weather),
33. BorderLayout.CENTER);
34. JPanel buttonPanel = new JPanel();
35. JButton reportButton = new JButton("Get report");
36. reportButton.addActionListener(new
37. ActionListener()
38. {
39. public void actionPerformed(ActionEvent evt)
40. {
41. weather.setText("");
42. new
43. Thread()
44. {
45. public void run()
46. {
47. getWeather(getItem(state, states),
48. getItem(report, reports));
49. }
50. }.start();
51. }
52. });
53.
54. buttonPanel.add(reportButton);
55. contentPane.add(buttonPanel, BorderLayout.SOUTH);
56. }
57.
58. /**
59. Makes a combo box.
60. @param items the array of strings whose first column
61. contains the combo box display entries
62. @param parent the parent to add the combo box to
63. */
64. public JComboBox makeCombo(String[][] items, Container parent)
65. {
66. JComboBox combo = new JComboBox();
67. for (int i = 0; i < items.length; i++)
68. combo.addItem(items[i][0]);
69. parent.add(combo);
70. return combo;
71. }
72.
73. /**
74. Gets the query string for a combo box display entry.
75. @param box the combo box
76. @param items the array of strings whose first column
77. contains the combo box display entries and whose second
78. column contains the corresponding query strings
79. @return the query string
80. */
81. public String getItem(JComboBox box, String[][] items)
82. {
83. return items[box.getSelectedIndex()][1];
84. }
85.
86. /**
87. Puts together the URL query and fills the text area with
88. the data.
89. @param state the state part of the query
90. @param report the report part of the query
91. */
92. public void getWeather(String state, String report)
93. {
94. String r = new String();
95. try
96. {
97. String queryBase = getParameter("queryBase");
98. String query
99. = queryBase + state + "/" + report + ".html";
100. URL url = new URL(query);
101. BufferedReader in = new BufferedReader(new
102. InputStreamReader(url.openStream()));
103.
104. String line;
105. while ((line = in.readLine()) != null)
106. weather.append(removeTags(line) + "\n");
107. }
108. catch(IOException e)
109. {
110. showStatus("Error " + e);
111. }
112. }
113.
114. /**
115. Removes HTML tags from a string.
116. @param s a string
117. @return s with <...> tags removed.
118. */
119. public static String removeTags(String s)
120. {
121. while (true)
122. {
123. int lb = s.indexOf('<');
124. if (lb < 0) return s;
125. int rb = s.indexOf('>', lb);
126. if (rb < 0) return s;
127. s = s.substring(0, lb) + " " + s.substring(rb + 1);
128. }
129. }
130.
131. private JTextArea weather;
132. private JComboBox state;
133. private JComboBox report;
134.
135. private String[][] states =
136. {
137. { "Alabama", "al" },
138. { "Alaska", "ak" },
139. { "Arizona", "az" },
140. { "Arkansas", "ar" },
141. { "California", "ca" },
142. { "Colorado", "co" },
143. { "Connecticut", "ct" },
144. { "Delaware", "de" },
145. { "Florida", "fl" },
146. { "Georgia", "ga" },
147. { "Hawaii", "hi" },
148. { "Idaho", "id" },
149. { "Illinois", "il" },
150. { "Indiana", "in" },
151. { "Iowa", "ia" },
152. { "Kansas", "ks" },
153. { "Kentucky", "ky" },
154. { "Louisiana", "la" },
155. { "Maine", "me" },
156. { "Maryland", "md" },
157. { "Massachusetts", "ma" },
158. { "Michigan", "mi" },
159. { "Minnesota", "mn" },
160. { "Mississippi", "ms" },
161. { "Missouri", "mo" },
162. { "Montana", "mt" },
163. { "Nebraska", "ne" },
164. { "Nevada", "nv" },
165. { "New Hampshire", "nh" },
166. { "New Jersey", "nj" },
167. { "New Mexico", "nm" },
168. { "New York", "ny" },
169. { "North Carolina", "nc" },
170. { "North Dakota", "nd" },
171. { "Ohio", "oh" },
172. { "Oklahoma", "ok" },
173. { "Oregon", "or" },
174. { "Pennsylvania", "pa" },
175. { "Rhode Island", "ri" },
176. { "South Carolina", "sc" },
177. { "South Dakota", "sd" },
178. { "Tennessee", "tn" },
179. { "Texas", "tx" },
180. { "Utah", "ut" },
181. { "Vermont", "vt" },
182. { "Virginia", "va" },
183. { "Washington", "wa" },
184. { "West Virginia", "wv" },
185. { "Wisconsin", "wi" },
186. { "Wyoming", "wy" }
187. };
188.
189. private String[][] reports =
190. {
191. { "Hourly (State Weather Roundup)", "hourly" },
192. { "State Forecast", "state" },
193. { "Zone Forecast", "zone" },
194. { "Short Term (NOWCASTS)", "shortterm" },
195. { "Forecast Discussion", "discussion" },
196. { "Weather Summary", "summary" },
197. { "Public Information", "public" },
198. { "Climate Data", "climate" },
199. { "Hydrological Data", "hydro" },
200. { "Watches", "watches" },
201. { "Special Weather Statements", "special" },
202. { "Warnings and Advisories", "allwarnings" }
203. };
204. }
Unfortunately, when you try running this applet, you will be disappointed. Both the applet viewer and the browser refuse to run it. Whenever you click on Get report, a security violation is generated. We discuss the reason for this violation, and how to overcome this problem, in the next section. Right now, we'll just show you how to force the applet viewer to run the applet anyway.
First, create a text file with the following contents:
grant
{
permission java.net.SocketPermission
"iwin.nws.noaa.gov:80", "connect";
};
Call it WeatherApplet.policy. We will discuss policy files in detail in Chapter 9. However, this particular policy file is easy to understand. It simply grants the applet the permission to connect to the host iwin.nws.noaa.gov on port 80, the HTTP port. Why the applet needs such a permission is the topic of the next section.
Now start the applet viewer with the following command line:
appletviewer -J-Djava.security.policy=WeatherApplet.policy
WeatherApplet.html
The -J option of the applet viewer passes command-line arguments to the Java bytecode interpreter. The -D option of the Java bytecode interpreter sets the value of a system property. Here, we set the system property java.security.policy to the name of a policy file that contains the security permissions for this program.
If you want to run the applet in a browser, you first have to sign it. Then, you and, more importantly, anyone else who wants to run it, must tell the browser:
To verify the signature (unless it was guaranteed by a trusted certificate authority);
To agree with the requested privileges.
We discuss applet signing in detail in Chapter 9.
As you just saw, your applet was forbidden from doing a very simple operation, namely, connecting to port 80 of a web server. In this section, we discuss the reason for this prohibition and describe how to deploy the applet without making your users fuss with certificates.
Web browsers allow an applet to read and write data only on the host that serves the applet. Applets can connect only to sockets on the computer from which the applet came. This rule is often described as "applets can only phone home."
At first, this restriction seems to make no sense. Why should the applet be denied an operation that the ambient browser carries out all the time, namely, to get data from a web host? To understand the rationale, it helps to visualize the three hosts involved, as shown in Figure 3-12:

The originating host — your computer that delivers the web page and Java applet to clients;
The local host — the user's machine that runs your applet;
The third-party data repository that your applet would like to see.
The applet security rule says that the applet can read and write data only on the originating host. Certainly it makes sense that the applet cannot write to the local host. If it could, it might be able to plant viruses or alter important files. After all, the applet starts running immediately when the user stumbles upon our web page, and the user must be protected from damage by malicious or incompetent applets.
It also makes sense that the applet cannot read from the
local host. Otherwise, it might browse the files on the local computer for
sensitive information, such as credit card numbers, open a socket connection to
the applet host, and write the information back. You might open a great-looking
web page, interact with an applet that does something fun or useful, and be
completely unaware of what that applet does in other threads. The browser denies
your applet all access to the files on your computer.

But why can't the applet read other files from the Web? Isn't the Web a wealth of publicly available information, made available for everyone to read? If you browse the Web from home, through a service provider, this is indeed the situation. But it is quite different when you do your Web surfing in your office (searching only for work-relevant information, of course). Many companies have their computer sitting behind a firewall.
A firewall is a computer that filters traffic going into and out of the corporate network. This computer will deny attempts to access services with less than stellar security histories. For example, there are known security holes in many FTP implementations. The firewall might simply disallow anonymous FTP requests or shunt them off to an isolated FTP server. It might also deny a request to access the mail port on all machines except the mail server. Depending on the security philosophy, the firewall (shown in Figure 3-13) can also apply filtering rules to the traffic between the corporate network and the Internet.

(If you are interested in this topic, turn to Firewalls and Internet Security: Repelling the Wily Hacker by William R. Cheswick and Steven M. Bellovin [Addison-Wesley, 1994].)
Having a firewall allows a company to use the Web to distribute internal information that is of interest to employees but should not be accessible outside the company. The company simply sets up a web server, tells the address only to its employees, and programs the firewall to deny any access requests to that server from the outside. The employees can then browse the internal information with the same web tools they already know and use.
If an employee visits your web page, the applet is downloaded into the computer behind the firewall and starts running there. If it were able to read all the web pages that the ambient browser can read, it would have access to the corporate information. Then, it could open a connection to the host from which it came and send all that private information back. That is obviously insecure. Since the browser has no idea which web pages are public and which are confidential, it disallows access to all of them.
That is too bad—you simply cannot write an applet that goes out on the Web, grabs information, processes and formats it, and presents it to the applet user. For example, our weather report applet does not want to write any information back to its host. Why doesn't the browser let the applet strike a deal? If the applet promises not to write anywhere, it ought to be able to read from everywhere. That way, it would just be a harvester and processor, showing an ephemeral result on the user's screen.
The trouble is that a browser cannot distinguish read from write requests. When you ask to open a stream on a URL, this is obviously a read request. Well, maybe not. The URL might be of the form
http://www.rogue.com/cgi-bin/cracker.pl?Garys+password+is+Sicily
Here, the culprit is the CGI mechanism. It is designed to take arbitrary arguments and process them. The script that handles the CGI request can, and often does, store the request data. It is easy to hide information in a CGI query text. (A data stream that contains hidden information is called a covert channel in security circles.)
So, should the browser disallow all CGI queries and allow access only to plain web pages? The solution is not that simple. The browser has no way of knowing that the server to which it connects on port 80 (the HTTP port) is actually a standard HTTP server. It might be just a shell that saves all requests to a file and returns an HTML page: "Sorry, the information you requested is not available." Then, the applet could transmit information by pretending to read from the URL
http://www.rogue.com/Garys/password/is/Sicily
Since the browser cannot distinguish read from write requests, it must disallow them both.
To summarize: Applets run on the computer browsing your web page, but they can connect only to the computer serving the web page. That is the "applets can only phone home" rule.
How, then, can you distribute an applet that harvests information for your users? You could make a web page that shows the applet in action with fake data, stored on your server. (This is exactly the approach that Sun takes with their stock-ticker sample applet.) You could then provide a button with which the user downloads the applet and a policy file. Users could then run it from the applet viewer.
That approach will probably greatly limit the attractiveness of your applet. Many people will be too lazy to download and install the applet locally if they did not first get to use it a few times with real data on your web page.
Fortunately, there is a way to feed the applet real data: install a proxy server on your HTML server. That proxy server is a service that grabs requested information from the Web and sends it to whoever requested it. For example, suppose your applet makes a GET request
http://www.yourserver.com/proxysvr?URL=http://iwin.nws.noaa.gov /iwin/CA/hourly.html
to the proxy server that resides on the same host as the applet code. Then the proxy server fetches the web page for the applet and sends it as the result of the GET request (see Figure 3-14).

Proxy servers for this purpose must exist, but we could not find one, so we wrote our own. You can see the code in Example 3-9. We implemented the proxy as a servlet, a program that is started by a servlet engine. Most web servers can either run servlets directly or can be extended to do so.
Servlets are not a part of the standard edition of the Java platform, and a detailed discussion of servlets is beyond the scope of this book. For details, please turn to Core Java Web Server by Chris Taylor and Tim Kimmet [Prentice-Hall 1998]. However, the servlet in Example 3-9 is quite simple. Here is a brief explanation of how it works.
When the web server receives a GET request for the servlet, it calls its doGet method. The HttpServletRequest parameter contains the request parameters. The servlet uses the getParameter method to get the value of the URL parameter. In any servlet, you send your response to the PrintStream that the getWriter method of the HttpServletResponse class returns. This servlet simply connects to the resource given in the URL parameter, reads the data a line at a time, and sends it to the response stream. Finally, you can see how the sendError method is used for error reporting.
The next section tells you how to configure the servlet if you use the Apache Tomcat Servlet Container. That software contains a small web server that you can run locally to test the weather report applet.
You don't have to use servlets to implement a proxy server. The sidebar tells you how you can implement the proxy server in C or Perl.
1. import java.io.*;
2. import java.net.*;
3. import javax.servlet.*;
4. import javax.servlet.http.*;
5.
6. /**
7. A very simple proxy servlet. The URL request parameter
8. specifies the resource to get. The servlet gets the
9. requested information and returns it to the caller.
10. */
11. public class ProxySvr extends HttpServlet
12. {
13. public void doGet(HttpServletRequest request,
14. HttpServletResponse response)
15. throws ServletException, IOException
16. {
17. String query = null;
18.
19. response.setContentType("text/html");
20. PrintWriter out = response.getWriter();
21.
22. query = request.getParameter("URL");
23. if (query == null)
24. {
25. response.sendError(HttpServletResponse.SC_BAD_REQUEST,
26. "Missing URL parameter");
27. return;
28. }
29.
30. try
31. {
32. query = URLDecoder.decode(query);
33. }
34. catch(Exception exception)
35. {
36. response.sendError(HttpServletResponse.SC_BAD_REQUEST,
37. "URL decode error " + exception);
38. return;
39. }
40.
41. try
42. {
43. URL url = new URL(query);
44. BufferedReader in = new BufferedReader(new
45. InputStreamReader(url.openStream()));
46.
47. String line;
48. while ((line = in.readLine()) != null)
49. out.println(line);
50. out.flush();
51. }
52. catch(IOException exception)
53. {
54. response.sendError(HttpServletResponse.SC_NOT_FOUND,
55. "Exception: " + exception);
56. }
57. }
58.}
1. #include <netdb.h>
2. #include <sys/types.h>
3. #include <sys/socket.h>
4. #include <netinet/in.h>
5. #include <arpa/inet.h>
6. #include <stdio.h>
7. #include <string.h>
8. #include <stdlib.h>
9.
10. #define MAXLINE 512
11. #define MAXNAME 128
12. #define HTTP 80
13.
14. unsigned writen(fd, vptr, n)
15. int fd;
16. char* vptr;
17. unsigned n;
18. {
19. unsigned nleft;
20. unsigned nwritten;
21. char* ptr;
22.
23. ptr = (char*)vptr;
24. nleft = n;
25. while (nleft > 0)
26. {
27. if ((nwritten = write(fd, ptr, nleft)) <= 0)
28. return nwritten;
29. nleft -= nwritten;
30. ptr += nwritten;
31. }
32. return n - nleft;
33. }
34.
35. unsigned readline(fd, vptr, maxlen)
36. int fd;
37. char* vptr;
38. int maxlen;
39. {
40. unsigned n;
41. unsigned rc;
42. char* ptr;
43. char c;
44.
45. ptr = vptr;
46. for (n = 1; n < maxlen; n++)
47. {
48. if ((rc = read(fd, &c, 1)) == 1)
49. {
50. *ptr++ = c;
51. if (c == '\n')
52. {
53. *ptr = 0;
54. return n;
55. }
56. }
57. else if (rc == 0)
58. {
59. if (n == 1) return 0;
60. else
61. {
62. *ptr = 0;
63. return n;
64. }
65. }
66. else
67. return -1;
68. }
69. *ptr = 0;
70. return n;
71. }
72.
73. void error(msg)
74. char* msg;
75. {
76. fputs(msg, stderr);
77. fputc('\n', stderr);
78. exit(1);
79. }
80.
81. void url_decode(in, out, outlen)
82. char* in;
83. char* out;
84. int outlen;
85. {
86. int i = 0;
87. int j = 0;
88. while (in[i] != '\0' && j < outlen - 1)
89. {
90. if (in[i] == '+') out[j] = ' ';
91. else if (in[i] == '%')
92. {
93. int ch;
94. sscanf(in + i + 1, "%x", &ch);
95. out[j] = ch;
96. i += 2;
97. }
98. else out[j] = in[i];
99. i++;
100. j++;
101. }
102. out[j] = 0;
103. }
104.
105. int main(argc, argv)
106. int argc;
107. char** argv;
108. {
109. int sockfd;
110. struct sockaddr_in serv_addr;
111. int n;
112. char* name;
113. struct hostent* hostptr;
114. char url[MAXLINE + 1];
115. char sendline[MAXLINE + 1];
116. char recvline[MAXLINE + 1];
117. char server_name[MAXNAME];
118. char file_name[MAXLINE];
119. int port;
120. int service = 0;
121. char* p;
122. char* q;
123.
124. url_decode(argv[1], url, sizeof(url));
125.
126. p = strstr(url, "URL=http://");
127. if (p != url)
128. error("Sorry--can only recognize URL=service://server/file");
129. service = HTTP;
130. p += strlen("URL=http://");
131. q = strchr(p, '/');
132. if (q == NULL)
133. error("Sorry--can only recognize //server/file");
134. strncpy(server_name, p, q - p);
135. server_name[q - p] = '\0';
136. strncpy(file_name, q, sizeof(file_name) - 1);
137. file_name[sizeof(file_name) - 1] = '\0';
138. port = service;
139.
140. if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
141. error("Can't open stream socket");
142.
143. bzero((char*)&serv_addr, sizeof(serv_addr));
144. serv_addr.sin_family = AF_INET;
145. hostptr = gethostbyname(server_name);
146. if (hostptr == 0) error("Can't find host");
147. name = inet_ntoa(*(struct in_addr*)*hostptr->h_addr_list);
148. serv_addr.sin_addr.s_addr = inet_addr(name);
149. serv_addr.sin_port = htons(port);
150.
151. if (connect(sockfd, (struct sockaddr*)&serv_addr,
152. sizeof(serv_addr)) < 0)
153. error("Can't connect to server");
154.
155. sendline[sizeof(sendline) - 1] = 0;
156. if (service == HTTP)
157. {
158. strcpy(sendline, "GET ");
159. strncat(sendline, file_name, sizeof(sendline) - 1
160. - strlen(sendline));
161. }
162. strncat(sendline, "\r\n", sizeof(sendline) - 1
163. - strlen(sendline));
164.
165. n = strlen(sendline);
166. if (writen(sockfd, sendline, n) != n)
167. error("Write error on socket");
168.
169. fputs("Content-type: text/html\n\n", stdout);
170.
171. do
172. {
173. n = readline(sockfd, recvline, MAXLINE);
174. if (n < 0)
175. error("Read error on socket");
176. else if (n > 0)
177. {
178. recvline[n] = 0;
179. fputs(recvline, stdout);
180. }
181. }
182. while (n > 0);
183.
184. return 0;
185. }
1. ($url) = @ARGV;
2.
3. $url =~ tr/+/ /;
4. $url =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
5.
6. $pos = index($url, "URL=http://");
7.
8. if ($pos != 0)
9. {
10. die "Sorry--can only recognize URL=http://server/file";
11. }
12.
13. $port = 80;
14.
15. $pos = 11;
16. $pos2 = index($url, "/", $pos);
17. if ($pos2 < 0)
18. {
19. die "Sorry--can only recognize //server/file";
20. }
21.
22. $server_name = substr($url, $pos, $pos2 - $pos);
23. $file_name = substr($url, $pos2);
24. $AF_INET = 2;
25. $SOCK_STREAM =1;
26.
27. $sockaddr = 'S n a4 x8';
28.
29. ($name, $aliases, $proto) = getprotobyname ('tcp');
30. ($name,$aliases,$type,$len,$thataddr)
31. = gethostbyname($server_name);
32. $that = pack($sockaddr, $AF_INET, $port, $thataddr);
33.
34. if (!socket (S, $AF_INET, $SOCK_STREAM, $proto))
35. {
36. die $!;
37. }
38.
39. if (!connect (S, $that))
40. {
41. die $!;
42. }
43.
44. select(S); $|=1; select(STDOUT);
45.
46. $command = "GET ".$file_name;
47.
48. print S $command."\r\n";
49.
50. print "Content-type: text/html\n\n";
51. while (<S>)
52. {
53. print;
54.
Comparing Servlets, Perl, and C for Server-side ProcessingCGI scripts can be written in any language that can read from standard input and write to standard output. The "traditional" choice for CGI scripts is Perl. However, we actually wrote our first proxy server script in C because we did not realize that Perl can connect to sockets. The Perl and C codes are listed in Example 3-10 and 3-11. As you can see, the C code is quite long. Even the most elementary operations (for example, reading a line of input) must be programmed in gory detail in C. Networking code is even worse. We didn't come up with the networking code ourselves. We modified a sample program from UNIX Network Programming, by W. Richard Stevens [Prentice-Hall, 1990]. Have a look at the code, and you will really appreciate the simplicity and elegance of the java.net package. The Perl code is much shorter but, as you can see by glancing at it, is completely unreadable to the uninitiated. There are charming variable names like $! and $| and inscrutable statements such as $url =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
This means "replace all strings of the form '% followed by two hex digits' by the corresponding hex value." (No, we didn't figure that out ourselves. Like everybody else, we copied it from another script.) The remainder of the program is a modification of an example from Programming Perl, (3rd edition) by Larry Wall, Tom Christiansen, and Jon Orwant [O'Reilly, 2000]. We don't pretend to understand the details, but it works. As a result of this experiment, we can heartily recommend servlets as a great mechanism for server-side processing. The network programming and string handling in the Java platform beat the daylights out of C, and the Java programming language code is far more readable and maintainable than the Perl equivalent. |
In this section, we show you how to test the weather report applet. You need a web server that can execute either servlets or CGI scripts. We give you detailed instructions for the Apache Tomcat servlet container. If you use another web server, you will need to make the appropriate adjustments.
Here are the steps.
Download Tomcat from http://jakarta.apache.org/tomcat/index.html.
Install Tomcat. To keep the instructions uncluttered, let's assume you installed it in the /tomcat directory. If you use another directory, modify the instructions accordingly.
Compile the servlet code:
javac -classpath /tomcat/lib/servlet.jar:. ProxySvr.java
Copy the class file ProxySvr.class to the /tomcat/webapps/examples/WEB-INF/classes directory.
Edit the /tomcat/webapps/examples/WEB-INF/web.xml file by adding the lines
<servlet>
<servlet-name>
ProxySvr
</servlet-name>
<servlet-class>
ProxySvr
</servlet-class>
</servlet>
and
<servlet-mapping>
<servlet-name>
ProxySvr
</servlet-name>
<url-pattern>
/ProxySvr
</url-pattern>
</servlet-mapping>
in the appropriate places. (Just follow the syntax of the other examples.)
Copy the files
WeatherApplet.html WeatherApplet*.class
to the /tomcat/webapps/examples directory. Note that there are several inner class files that you need to copy.
Edit the WeatherApplet.html file and change the PARAM tag to
<PARAM NAME="queryBase" VALUE= "http://localhost:8080/examples/ProxySvr?URL=http:// iwin.nws.noaa.gov/iwin/">
Start Tomcat.
Start the applet by running
appletviewer http://localhost:8080/examples/WeatherApplet.html
Alternatively, use your browser to view that URL. Your browser needs to be able to handle Java 2 applets (such as Netscape 6 or Opera). Note that no security policy file is required
Now that you have seen the applet in action, let us review why the proxy server solves the applet security problem. When the user selects a state and report type, the weather applet requests the information from the proxy server on its local host. The proxy server then goes out to the National Weather Service, gets the data, and feeds it back to the applet.
This looks like a lot of trouble, but we have avoided the security risk. The applet only talks to the proxy server. The proxy server cannot peek at documents that may be accessible from the machine that is running the applet.
You can use this technique whenever you want to deploy an applet that harvests information from other web sites. In effect, you have offloaded the harvesting to the server, and you use the applet for presenting the results.
Now the security monkey is on your back. By installing the proxy server on your web server, you enable anyone to access it and download any files that are visible from your server. It is then up to you to configure your network so that the proxy server cannot pick up any of your confidential files. Alternatively, you can implement the proxy server so that it retrieves only URLs of a certain form. In our case, a useful restriction would be to reject any requests other than for URLs starting with http://iwin.nws.noaa.gov.
Does it make sense to have the server merely grab the information and reflect it to the applet? In this case, it does, but in general, it often makes sense for the server to cache it, thus improving performance when there are multiple requests, or to preprocess the information. In this example, we used server-side processing to avoid a security issue. In many other situations, it is best to process as much information as possible on the server because the server software is easier to control and maintain. Applets still have their place—interacting with the user and presenting results. In a typical 3-tier (or n-tier) application, the applet is paired with a servlet or other server-side programming mechanism that does the "heavy lifting."
| CONTENTS |